//
//  ProductOrdersListCell.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/5/6.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "ProductOrdersListCell.h"

#import "ProductOrderCell.h"

@interface ProductOrdersListCell()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UIView *bgView;

@property (nonatomic,strong) UIImageView *logoImgV;

@property (nonatomic,strong) UILabel *nameLB;

@property (nonatomic,strong) UIButton *toDetailBtn;

@property (nonatomic,strong) UILabel *orderStatusLB;//订单状态lb

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataSourceArr;

@property (nonatomic,strong) UIView *orderInfoView;

@property (nonatomic,strong) UILabel *countDownLB;//订单待支付倒计时
@property (nonatomic,strong) UILabel *orderNumberLB;//订单数量lb
@property (nonatomic,strong) UILabel *orderPriceLB;//订单价格lb
@property (nonatomic,strong) UIButton *orderBtn1;
@property (nonatomic,strong) UIButton *orderBtn2;
@property (nonatomic,strong) UIButton *orderBtn3;


@end

@implementation ProductOrdersListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = MainBackColor;
        self.backgroundColor = MainBackColor;
        
        self.dataSourceArr = [NSMutableArray array];
        
        [self initUI];
    }
    return self;
}
    
#pragma mark - setter
- (void)setModel:(ProductOrderListModel *)model{
    _model = model;
    
    self.bgView.height = _model.cellHeight - 12;
    self.tableView.height = _model.cellHeight - 158;
    self.orderInfoView.top = self.tableView.bottom;
    
    self.nameLB.text = _model.company;
    self.nameLB.width = [Configuration getLabelWidthWithText:self.nameLB.text withFont:self.nameLB.font];
    self.toDetailBtn.left = self.nameLB.right + 5;
    
    self.orderPriceLB.text = [NSString stringWithFormat:@"实付款： %.01fTB",_model.totaltbprice];
    self.orderNumberLB.text = [NSString stringWithFormat:@"共%ld件",_model.number];
    self.orderStatusLB.text = _model.status;
    
    if (_model.countdown > 0) {
//        self.countDownLB.hidden = NO;
        //开启定时器
//        [self startCountDownWithTime:_model.countdown];
    }else{
        self.countDownLB.hidden = YES;
    }
    
    self.dataSourceArr = [NSMutableArray arrayWithArray:_model.items];
    [self.tableView reloadData];
    
    self.orderBtn1.hidden = NO;
    [self.orderBtn1 setTitle:@"查看详情" forState:normal];
    NSMutableArray *btnTitlesArr = [NSMutableArray array];
    if (_model.receivebtn == YES) {
        [btnTitlesArr addObject:@"确认收货"];
    }
    if (_model.paybtn == YES) {
        [btnTitlesArr addObject:@"去支付"];
    }
    if (_model.cancelbtn == YES) {
        [btnTitlesArr addObject:@"取消订单"];
    }
    if (_model.deletebtn == YES) {
        [btnTitlesArr addObject:@"删除订单"];
    }
    if (_model.trackbtn == YES) {
        [btnTitlesArr addObject:@"查看物流"];
    }
    if (btnTitlesArr.count >= 2) {
        self.orderBtn2.hidden = NO;
        [self.orderBtn2 setTitle:btnTitlesArr[0] forState:normal];
    }else if (btnTitlesArr.count >= 3) {
        self.orderBtn2.hidden = NO;
        self.orderBtn3.hidden = NO;
        [self.orderBtn2 setTitle:btnTitlesArr[0] forState:normal];
        [self.orderBtn3 setTitle:btnTitlesArr[1] forState:normal];
    }
}
//开启倒计时定时器
-(void)startCountDownWithTime:(NSInteger)countTime{
    WEAKSELF(self);
    __block NSInteger timeOut = countTime/1000;
    //创建globle队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //创建timer 定时器
//    dispatch_source_cancel(_timer);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //每秒执行。 设置1s触发一次，0s的误差
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        if(timeOut<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer); //取消 dispatch 源
            dispatch_async(dispatch_get_main_queue(), ^{
                //回主线程  更新UI   设置界面的按钮显示 根据自己需求设置
                weakSelf.countDownLB.hidden = YES;
            });
        }else{
            int seconds = timeOut % 60;
            int minite = timeOut/60;
            dispatch_async(dispatch_get_main_queue(), ^{
                //回主线程 更新UI   设置界面的按钮显示 根据自己需求设置
                weakSelf.countDownLB.text = [NSString stringWithFormat:@"支付时间：%.2d:%.2d",minite,seconds];
//                NSLog(@"%@",[NSString stringWithFormat:@"支付时间：%.2d:%.2d",minite,seconds]);
            });
            timeOut--;
        }
    });
    //开始执行dispatch源
    dispatch_resume(_timer);
}

#pragma mark - UI
-(void)initUI{
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.logoImgV];
    [self.bgView addSubview:self.nameLB];
    [self.bgView addSubview:self.toDetailBtn];
    [self.bgView addSubview:self.orderStatusLB];
    [self.bgView addSubview:self.tableView];
    [self.bgView addSubview:self.orderInfoView];
    
    [self.orderInfoView addSubview:self.countDownLB];
    [self.orderInfoView addSubview:self.orderPriceLB];
    [self.orderInfoView addSubview:self.orderNumberLB];
    [self.orderInfoView addSubview:self.orderBtn1];
    [self.orderInfoView addSubview:self.orderBtn2];
    [self.orderInfoView addSubview:self.orderBtn3];
}
#pragma mark - lazy
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 12, ScreenWidth, 210)];
        _bgView.clipsToBounds = YES;
        _bgView.layer.cornerRadius = 8;
        _bgView.backgroundColor = UIColor.whiteColor;
    }
    return _bgView;
}
- (UIImageView *)logoImgV{
    if (!_logoImgV) {
        _logoImgV = [[UIImageView alloc] initWithFrame:CGRectMake(16, 17, 15, 15)];
        _logoImgV.image = LoadImage(@"商品详情_店铺");
    }
    return _logoImgV;
}

- (UILabel *)nameLB{
    if (!_nameLB) {
        _nameLB = [[UILabel alloc] initWithFrame:CGRectMake(self.logoImgV.right + 4, self.logoImgV.top - 2, 80, 20)];
        _nameLB.textColor = MainBlackTextColor;
        _nameLB.font = FontSet(15);
        _nameLB.textAlignment = NSTextAlignmentLeft;
        
        _nameLB.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toStoreClick:)];
        tap.delegate = self;
        [_nameLB addGestureRecognizer:tap];
    }
    return _nameLB;
}
- (UIButton *)toDetailBtn{
    if (!_toDetailBtn) {
        _toDetailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _toDetailBtn.frame = CGRectMake(self.nameLB.right + 5, self.nameLB.top, 40, 20);
        [_toDetailBtn setImage:LoadImage(@"个人中心_查看全部") forState:normal];
        [_toDetailBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 34)];
        [_toDetailBtn addTarget:self action:@selector(toDetailClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _toDetailBtn;
}

- (UILabel *)orderStatusLB{
    if (!_orderStatusLB) {
        _orderStatusLB = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 95, self.toDetailBtn.top - 1, 80, 18)];
        _orderStatusLB.textColor = RGB(255, 34, 21);
        _orderStatusLB.font = FontSet(15);
        _orderStatusLB.textAlignment = NSTextAlignmentRight;
    }
    return _orderStatusLB;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, ScreenWidth, 110) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.scrollEnabled = NO;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.1f)];
        [_tableView setTableHeaderView:view];
    }
    return _tableView;
}
- (UIView *)orderInfoView{
    if (!_orderInfoView) {
        _orderInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, self.tableView.bottom, ScreenWidth, 95)];
        _orderInfoView.backgroundColor = UIColor.whiteColor;
    }
    return _orderInfoView;
}

- (UILabel *)countDownLB{
    if (!_countDownLB) {
        _countDownLB = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, ScreenWidth/2, 18)];
        _countDownLB.font = FontSet(13);
        _countDownLB.textColor = MainGrayTextColor;
        _countDownLB.textAlignment = NSTextAlignmentLeft;
        _countDownLB.hidden = YES;
    }
    return _countDownLB;
}

- (UILabel *)orderPriceLB{
    if (!_orderPriceLB) {
        _orderPriceLB = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 150, 12, 135, 18)];
        _orderPriceLB.font = FontSet(13);
        _orderPriceLB.textColor = MainBlackTextColor;
        _orderPriceLB.textAlignment = NSTextAlignmentLeft;
    }
    return _orderPriceLB;
}
- (UILabel *)orderNumberLB{
    if (!_orderNumberLB) {
        _orderNumberLB = [[UILabel alloc] initWithFrame:CGRectMake(self.orderPriceLB.left - 120, 12, 110, 18)];
        _orderNumberLB.font = FontSet(13);
        _orderNumberLB.textColor = MainGrayTextColor;
        _orderNumberLB.textAlignment = NSTextAlignmentRight;
    }
    return _orderNumberLB;
}

- (UIButton *)orderBtn1{
    if (!_orderBtn1) {
        _orderBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        _orderBtn1.frame = CGRectMake(ScreenWidth - 100, self.orderPriceLB.bottom+24, 84, 30);
        _orderBtn1.clipsToBounds = YES;
        _orderBtn1.layer.cornerRadius = 15;
        [_orderBtn1 setTitleColor:RGB(255, 34, 21) forState:normal];
        _orderBtn1.layer.borderWidth = 1;
        _orderBtn1.layer.borderColor = RGBA(255, 34, 21, 0.5f).CGColor;
        [_orderBtn1.titleLabel setFont:FontSet(14)];
        [_orderBtn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _orderBtn1.hidden = YES;
    }
    return _orderBtn1;
}
- (UIButton *)orderBtn2{
    if (!_orderBtn2) {
        _orderBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        _orderBtn2.frame = CGRectMake(self.orderBtn1.left - 100, self.orderBtn1.top, 84, 30);
        _orderBtn2.clipsToBounds = YES;
        _orderBtn2.layer.cornerRadius = 15;
        [_orderBtn2 setTitleColor:MainPureColor(119) forState:normal];
        _orderBtn2.layer.borderWidth = 1;
        _orderBtn2.layer.borderColor = MainPureColor(220).CGColor;
        [_orderBtn2.titleLabel setFont:FontSet(14)];
        [_orderBtn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _orderBtn2.hidden = YES;
    }
    return _orderBtn2;
}
- (UIButton *)orderBtn3{
    if (!_orderBtn3) {
        _orderBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
        _orderBtn3.frame = CGRectMake(self.orderBtn2.left - 100, self.orderBtn1.top, 84, 30);
        _orderBtn3.clipsToBounds = YES;
        _orderBtn3.layer.cornerRadius = 15;
        [_orderBtn3 setTitleColor:MainPureColor(119) forState:normal];
        _orderBtn3.layer.borderWidth = 1;
        _orderBtn3.layer.borderColor = MainPureColor(220).CGColor;
        [_orderBtn3.titleLabel setFont:FontSet(14)];
        [_orderBtn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _orderBtn3.hidden = YES;
    }
    return _orderBtn3;
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProductOrderCell"];
    if (!cell) {
        cell = [[ProductOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ProductOrderCell"];
    }
    cell.model = self.dataSourceArr[indexPath.row];
    return cell;
}
#pragma mark - SEL
//进入店铺
-(void)toStoreClick:(UIGestureRecognizer *)tap{
    [self toDetailClick];
}
//进入店铺
-(void)toDetailClick{
}
//底部按钮点击事件
-(void)btnClick:(UIButton *)btn{
    if (self.touchBlock) {
        self.touchBlock(self.model, btn.titleLabel.text);
    }
}


@end




